﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;
using System.Collections.Specialized;

namespace BTSZap
{
    

      

    public static class BiztalkWMIOperations
    {
        
        public static void RestartHostInstances(string dbServer,string dbName)
        {
            
                //Create EnumerationOptions and run wql query
                EnumerationOptions enumOptions = new EnumerationOptions();
                enumOptions.ReturnImmediately = false;
                dbServer.Replace(@"\", @"\\");
                //Search for all HostInstances of 'InProcess' type in the Biztalk namespace scope
                ManagementObjectSearcher searchObject = new ManagementObjectSearcher("root\\MicrosoftBizTalkServer", string.Format("Select * from MSBTS_Host where MgmtDbServerOverride='{0}' AND MgmtDbNameOverride='{1}' AND HostType=1",dbServer,dbName), enumOptions);

                //Enumerate through the result set and start each HostInstance if it is already stopped
                foreach (ManagementObject inst in searchObject.Get())
                {
                    inst.InvokeMethod("Stop", null);
                    inst.InvokeMethod("Start", null);                    
                }                                         

        }
        public static StringCollection GetServers(string dbServer, string dbName)
        {
            dbServer.Replace(@"\", @"\\");
            StringCollection servers = new StringCollection();
            //Create EnumerationOptions and run wql query
            EnumerationOptions enumOptions = new EnumerationOptions();
            enumOptions.ReturnImmediately = false;
            //Search for all HostInstances of 'InProcess' type in the Biztalk namespace scope
            ManagementObjectSearcher searchObject = new ManagementObjectSearcher("root\\MicrosoftBizTalkServer", string.Format("Select * from MSBTS_HostInstance where MgmtDbServerOverride='{0}' AND MgmtDbNameOverride='{1}' AND HostType=1", dbServer, dbName), enumOptions);

            //Enumerate through the result set and start each HostInstance if it is already stopped
            foreach (ManagementObject inst in searchObject.Get())
            {
                string serverName = (string) inst["RunningServer"];
                if ((!string.IsNullOrEmpty(serverName)) && (!servers.Contains(serverName)))
                    servers.Add(serverName);
            }
            return servers;
        }
        public static bool UninstallMSI(string server, string MSIName)
        {
            EnumerationOptions enumOptions = new EnumerationOptions();
            enumOptions.ReturnImmediately = false;
            Log.StartWait();
            try
            {
                 ManagementObjectSearcher searchObject = new ManagementObjectSearcher(string.Format(@"\\{0}\ROOT\CIMV2",server),
                    string.Format("SELECT * from Win32_Product where PackageName=\"{0}\"",MSIName));
                ManagementObjectCollection coll =  searchObject.Get();
                if (coll == null) return false;
                if (coll.Count != 1) return false;
                uint resu = 1;
                foreach (ManagementObject inst in coll)
                {
                    object o = inst.InvokeMethod("Uninstall", new object[] { });
                    resu = (uint)o;
                }
                return (resu == 0);
            }
            finally
            {
                Log.StopWait();
            }
        }
    }
}
